<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/20 0020
 * Time: 下午 4:05
 */
include('dayu/top/TopClient.php'); //这个是你下面实例化的类
include('dayu/top/ResultSet.php'); //这个是topClient 里面需要实例化一个类所以我们也要加载 不然会报错
include('dayu/top/RequestCheckUtil.php'); //这个是成功后返回的信息文件
include('dayu/top/TopLogger.php');   //这个是错误信息返回的一个php文件
include('dayu/top/request/AlibabaAliqinFcSmsNumSendRequest.php'); //这个也是你下面示例的类*/
require('conn.php');
require('functions.php');
$lifeTime = 300;//cookie的生命周期
session_set_cookie_params($lifeTime);
session_start();

//stdClass Object 转 数组
function objectArray($array)
{
    if (is_object($array)) {
        $array = (array)$array;
    }
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            $array[$key] = objectArray($value);
        }
    }
    return $array;
}

if ($_GET) {
    $token = guolv($_GET['ip']);
    $phone = guolv($_GET['phone']);
    $ip = GetIP();
    $hashToken = md5($ip."CNM");
    if($hashToken != $token){
        echo 2;
        exit;
    }
    $day = date("Y-m-d", time());
    //注册信息
    $ipTimes = $mysql->query("select COUNT(*) as iptimes from `register` WHERE  `ip`='{$ip}' AND `day`='{$day}'");
    //同一ip一天只能收此次验证码4次
    if($ipTimes[0]['iptimes'] >= 4){
        echo 2;
        exit;
    }
    $regInfo = $mysql->query("select * from `register` WHERE `phone`='{$phone}' AND `day`='{$day}'");
    if($regInfo){
        $times = $regInfo[0]['times'];
        if($times <= 0){
            echo 2;//操作太频繁
            exit;
        }
        $res = $mysql->execute("update `register` set `times`=`times`-1 WHERE `phone`='{$phone}' AND `day`='{$day}'");
    }else{
        $userInfo = [
            'phone' => $phone,
            'ip' => $ip,
            'day' => $day,
        ];
        $regSql = "insert into `register`" . arr2s($userInfo);
        $res = $mysql->execute($regSql);
    }
    if (!$_COOKIE['yzm_time']) {
        $row = $mysql->query("select * from `user` where `phone`='{$phone}' limit 1");
        if (!$row) {
            $rand = rand(100000, 999999);
            $msg = "";
            $ym = "【有妹纸】";
            /*实例化大鱼接口*/
            $c = new TopClient;
            $c->appkey = "23352695";
            $c->secretKey = "b2b10433487dc7a27f6cc82aea034d1f";
            $req = new AlibabaAliqinFcSmsNumSendRequest;
            $req->setExtend("123456");
            $req->setSmsType("normal");
            $req->setSmsFreeSignName("注册验证");
            $req->setSmsParam("{\"code\":\"$rand\",\"product\":\"$ym\"}");
            $req->setRecNum("$phone");
            $req->setSmsTemplateCode("SMS_8220695");
            //执行
            $resp = $c->execute($req);
            //大鱼返回值
            $res = objectArray($resp);
            if ($res['result']['success']) {
                setcookie("phone", $phone, time() + 300);
                setcookie("yzm", md5($rand), time() + 300);
                echo 1;//验证码发送成功
            } else {
                echo 2;//用户操作过于频繁,验证码发送失败
            };
        } else {
            echo 0;//用户已存在
        }
    } else {
//        $_SESSION['yzm_time'];
        echo 3;//再次发送验证码时间
    }

}


if ($_POST) {
    $day = date("Y-m-d", time());
    $ip = GetIP();
    //注册
    $yzm = guolv($_POST['yzm_value']);
    $phone = guolv($_POST['phone']);
    $password = guolv($_POST['password']);
    $tj_id = guolv(trim($_POST['tj_id']));//推荐人
    $device = guolv($_POST['device']);
    if (is_numeric($tj_id)) {
        $row_tj = $mysql->query("select * from `user` where `id` in({$tj_id})");
        if (!$row_tj) {
            $tj_id = 0;
        }
    } else {
        $tj_id = 0;
    }
    if (md5($yzm) == guolv($_COOKIE['yzm']) && $phone == guolv($_COOKIE['phone'])) {
        $row = $mysql->query("select * from `user` where `phone`=$phone limit 1");
        if (!$row) {
            $arr = array(
                'pid' => $tj_id,
                'phone' => $phone,
                'password' => $password,
                'ip' => $ip,
                'gold' => '3',
                'day' => date("Y-m-d H:i:s", time()),
                'device' => $device,
            );
            $value = arr2s($arr);
            $day = date("Y-m-d", time());
            $time = time();
            $mysql->query("insert into `user` {$value}");
            $id = mysql_insert_id();
            $authArr = [
                'user_id' => $id,
            ];
            $auth = arr2s($authArr);
            $mysql->query("insert into `jhi_user_authority` {$auth}");
            //注册成功
            if ($id !== 0) {
                echo 1;//注册成功
            } else {
                echo 3;//注册失败
            }
        } else {
            echo 2;//用户已存在
        }
    } else {
        echo 0;//信息验证失败
    }
}